package view;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import controller.UnionDueController;
import controller.EmployeeController;
import entity.Employee;
import entity.Union;
import entity.UnionDue;

@ManagedBean
@ViewScoped
public class UnionServiceView {

	@Inject
	EmployeeController employeeController;
	@Inject
	UnionDueController unionDueController;

	private UnionDue uniondue = new UnionDue();
	private Employee employee;
	private Union union;
	private List<UnionDue> listUnionDueEmployee;
	private List<UnionDue> listDeleteUnionDue=new ArrayList<UnionDue>();
	@PostConstruct
	public void ServiceView() {
		FacesContext context = FacesContext.getCurrentInstance();
		String id = context.getExternalContext().getRequestParameterMap()
				.get("SendEmployee");
		this.employee = employeeController.findEmployee(Integer.parseInt(id));
		this.union=(Union)employee.getAssociation();
		listUnionDueEmployee=unionDueController.ListUnionDueEmployee(employee);
	}

	public String AddNewUnionService() {

		UnionDue due = unionDueController.RegisterNewUnionService(
				uniondue.getDate(), uniondue.getAmount(),
				(Union) employee.getAssociation());
		String message;
		if (due == null) {
			message = new String("Problema Dati non aggiunti");
		} else {
			message = new String("UnionService");
			uniondue=new UnionDue();
		}
		return message;
	}

	
	public void addDeleteUnion(UnionDue unionDue){
		System.out.println("Fatto");
		if(!listDeleteUnionDue.contains(unionDue)){
			listDeleteUnionDue.add(unionDue);
		}else{
			listDeleteUnionDue.remove(unionDue);
		}
		
	}
	public void DeleteUnionDues(){
		System.out.println("ciaoSonoDentro"); 
		int value;
		for(int i=0;i<listDeleteUnionDue.size();i++){
			value=unionDueController.removeUnionDue(listDeleteUnionDue.get(i));
		}
		listDeleteUnionDue.clear();
	}
	public List<UnionDue> getListDeleteUnionDue() {
		return listDeleteUnionDue;
	}

	public void setListDeleteUnionDue(List<UnionDue> listDeleteUnionDue) {
		this.listDeleteUnionDue = listDeleteUnionDue;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public UnionDue getUniondue() {
		return uniondue;
	}

	public void setUniondue(UnionDue uniondue) {
		this.uniondue = uniondue;
	}
	
	public Union getUnion() {
		return union;
	}

	public void setUnion(Union union) {
		this.union = union;
	}

	
	public List<UnionDue> getListUnionDueEmployee() {
		return listUnionDueEmployee;
	}

	public void setListUnionDueEmployee(List<UnionDue> listUnionDueEmployee) {
		this.listUnionDueEmployee = listUnionDueEmployee;
	}

}
